/*
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Authors:
 *  	Albert Marieges Garcia
 *  	Álex Pardo Fernández
 *  	Enric Mayas Márquez
 *  	Juan Carlos Calvo Tejedor
 *  	Marc Gironés Dezsényi
 *  
 *  Contact:
 *  	bcn.tours.android@gmail.com
 *  Webpage:
 *  	bcntours.co.cc
 *
 */
package com.BCNTours.Maps.Utils;

import java.util.ArrayList;

import android.graphics.drawable.Drawable;

import com.BCNTours.Maps.Vista.MapMode;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.OverlayItem;

/**
 * Support class that manages the overlays contained by a mapview.
 */

@SuppressWarnings({ "rawtypes" })
public class OverlayManager extends ItemizedOverlay {
	//Attributes
	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	private MapActivity map;
	private boolean clickable;

	/**
	 * Constructor of the class.
	 * 
	 * @param Icon that will represent the marker
	 * @param Map where the icon will be drawn
	 */
	public OverlayManager(Drawable defaultMarker, MapActivity map, boolean clickable) {
		super(boundCenterBottom(defaultMarker));
		this.clickable = clickable;
		this.map = map;
	}

	@Override
	protected OverlayItem createItem(int i) {
	  return mOverlays.get(i);
	}

	@Override
	public int size() {
		return mOverlays.size();
	}
	
	public void addOverlay(OverlayItem overlay) {
	    mOverlays.add(overlay);
	}
	
	public void addOverlay(OverlayItem overlay, Drawable marker) {
		overlay.setMarker(marker);
	    mOverlays.add(overlay);
	}

	/**
	 * Draws the overlays contained in the OverlayManager on the map specified at the Constructor.
	 */
	public void draw(){
		populate();	
	}
	
	@Override
	protected boolean onTap(int index) {
	  if(clickable){
		  //Shows a dialog with the options related to the point of interest that has been tapped
		  final OverlayItem item = mOverlays.get(index);
		  MapMode mapMode = (MapMode) map;
		  mapMode.showDialog(item);
	  }
	  return true;
	}

	/**
	 * Clear the OverlayManager
	 */
	public void removeOverlays() {
		mOverlays = new ArrayList<OverlayItem>();
	}
}
